#include<event2/event.h>
#include<event2/listener.h>
#include<iostream>
#include<string.h>
#include <signal.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <zlib/zlib.h>
using namespace std;
#define FILEPATH "001.txt"
struct ClientStatus{
    FILE *fp=0;
    bool end=false;
};
static void Client_read_cb(bufferevent *bev,void *arg)
{
    char data[1024]={0};
    bufferevent_read(bev,data,sizeof(data)-1);
    if(strcmp(data,"ok")==0)
    {
        cout<<"Client:"<<data<<endl;
        bufferevent_trigger(bev,EV_WRITE,0);
    }
    else
    {
        bufferevent_free(bev);
    }
    
}
static void Client_write_cb(bufferevent *bev,void *arg)
{
    //发送文件
    ClientStatus *cs=(ClientStatus*)arg;
    cout<<"Client_write_cb"<<endl;
    FILE *fp=cs->fp;
    //判断什么时候清理资源
    if(cs->end)
    {
         //判断缓冲是否有数据,如果有就刷新
        //获取过滤器绑定的buffer
        bufferevent *be = bufferevent_get_underlying(bev);
        //获取输出缓冲及其大小
        evbuffer *evb= bufferevent_get_output(be);
        int len=evbuffer_get_length(evb);
        if(len<=0)
        {
            bufferevent_free(bev);
            delete cs;
            return;
        }
        //bufferevent_free(bev);
        bufferevent_flush(bev,EV_WRITE,BEV_FINISHED);
       return;
    }

    if(!fp)return;
    char data[1024]={0};
    int len = fread(data,1,sizeof(data),fp);
    if(len<=0)
    {
        fclose(fp);
        cs->end=true;
        bufferevent_flush(bev,EV_WRITE,BEV_FINISHED);
        return;
    }
    bufferevent_write(bev,data,len);
   
    
}
static bufferevent_filter_result filter_in(evbuffer *s,evbuffer *d,ev_ssize_t limit,bufferevent_flush_mode mode,void *arg)
{

    return BEV_OK;
}
static bufferevent_filter_result filter_out(evbuffer *s,evbuffer *d,ev_ssize_t limit,bufferevent_flush_mode mode,void *arg)
{
    char data[1024]={0};
    int len=evbuffer_remove(s,data,sizeof(data));
    evbuffer_add(d,data,len);

    return BEV_OK;
}
void Client_event_cb(bufferevent *bev,short events,void *arg)
{
    cout<<"client_event_cb"<<events<<endl;
    if(events & BEV_EVENT_CONNECTED)
    {
        cout<<"BEV_EVENT_CONNECTED"<<endl;
        bufferevent_write(bev,FILEPATH,strlen(FILEPATH));
        //创建输出过滤
        bufferevent *bev_filter= bufferevent_filter_new(bev,
            0,
            filter_out,
            BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS,
            0,
            0
        );
        FILE *fp=fopen(FILEPATH,"rb");
        if(!fp)
        {
            cout<<"file open failed"<<endl;
            exit(1);
        }
        ClientStatus *cs=new ClientStatus();
        cs->fp=fp;

        //设置回调函数
        bufferevent_setcb(bev_filter,Client_read_cb,Client_write_cb,0,cs);
        bufferevent_enable(bev_filter,EV_READ|EV_WRITE);
    }

}


void Client(event_base *base){
    cout<<"Client begin"<<endl;
    //连接服务器
    sockaddr_in sin;
    memset(&sin,0,sizeof(sin));
    sin.sin_family=AF_INET;
    sin.sin_port=htons(6666);
    evutil_inet_pton(AF_INET,"127.0.0.1",&sin.sin_addr.s_addr);
    bufferevent *bev=bufferevent_socket_new(base,-1,BEV_OPT_CLOSE_ON_FREE);

    //绑定事件回调,用于确定连接成功
    bufferevent_enable(bev,EV_READ|EV_WRITE);
    bufferevent_setcb(bev,0,0,Client_event_cb,base);
    bufferevent_socket_connect(bev,(sockaddr *)&sin,sizeof(sin));

    //发送文件名
    //接收回复确认ok
}